package com.moviesearch.elastic7.controller;

import com.moviesearch.elastic7.model.Filmresources;
import com.moviesearch.elastic7.service.ElasticIndexService;
import com.moviesearch.elastic7.service.ElasticService;
import com.moviesearch.elastic7.util.ResultUitl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
public class ElasticController {
    @Resource
    private ElasticService ElasticService;
    @Resource
    private ElasticIndexService elasticIndexService;
    /*将数据库的数据转移到elastic中*/
    @RequestMapping("mysqltoes")
    public ResultUitl mysqlToEs(String index) throws IOException {
        String result = ElasticService.mysqlToEs(index);
        return new ResultUitl(null,null,result,"01");
    }
    /*新增一条数据*/
    @RequestMapping("insertEs")
    public ResultUitl insertEs(Filmresources filmresources,String index) throws IOException {
        String result = ElasticService.insertEs(filmresources,index);
        return new ResultUitl(null,null,result,"01");
    }

    /*修改一条数据*/
    @RequestMapping("updateEs")
    public  ResultUitl updateEs(Filmresources filmresources,String index,String esid) throws  IOException{
        String result = ElasticService.updateEs(filmresources,index,esid);
        return new ResultUitl(null,null,result,"01");
    }
    /*查询一条数据*/
    @RequestMapping("getEs")
    public  ResultUitl getEs(String index,String esid) throws  IOException{
        Filmresources filmresources = ElasticService.getEs(index,esid);
        return new ResultUitl(filmresources,null,"success","01");
    }
    /*删除一条数据*/
    @RequestMapping("deleteEs")
    public  ResultUitl deleteEs(String index,String esid) throws IOException{
        String result = ElasticService.deleteEs(index, esid);
        return new ResultUitl(null,null,result,"01");
    }

    /*根据条件查询数据*/
    @RequestMapping("getbycondition")
    public  ResultUitl getByCondition(String index, String keyword , Integer pageIndex, Integer pageCount)throws IOException{
        List<Filmresources> byCondition = ElasticService.getByCondition(index, keyword, pageIndex, pageCount,
                "filmtitle", "filmactors", "filmdescription");
        return new ResultUitl(null,byCondition,"success","01");
    }

    /*根据条件高亮查询数据*/
    @RequestMapping("getbyconditionhigh")
    public  ResultUitl getbyconditionhigh(String index, String keyword , Integer pageIndex, Integer pageCount)throws IOException{
        List<Filmresources> byCondition = ElasticService.getByConditionHigh(index, keyword, pageIndex, pageCount,
                "filmtitle", "filmactors", "filmdescription");
        return new ResultUitl(null,byCondition,"success","01");
    }

    /*创建mapping*/
    @RequestMapping("createmapping")
    public  ResultUitl createmapping(String index)throws IOException{
        String result = elasticIndexService.createIndex(index);
        return new ResultUitl(null,null,result,"01");
    }

    /*判断index是否存在*/
    @RequestMapping("indexexits")
    public  ResultUitl indexExits(String index)throws IOException{
        boolean result = elasticIndexService.hasIndex(index);
        return new ResultUitl(null,null,result+"","01");
    }

    /*删除index*/
    @RequestMapping("deleteIndex")
    public ResultUitl deleteIndex(String index) throws IOException {
        String result = elasticIndexService.deleteIndex(index);
        return new ResultUitl(null,null,result,"01");
    }
}
